package com.qiku.controller;

import com.qiku.domain.PromotionSpace;
import com.qiku.domain.ResponseResult;
import com.qiku.service.PromotionSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/PromotionSpace")
public class PromotionSpaceController {
    @Autowired
    private PromotionSpaceService promotionSpaceService;

    /**
     * 获取广告位列表数据
     * @return
     */
    @GetMapping("/findAllPromotionSpace")
    public ResponseResult findAllPromotionSpace() {
        ResponseResult result = null;
        try {
            List<PromotionSpace> promotionSpaceList = promotionSpaceService.findAllPromotionSpace();
            result = new ResponseResult(true, 200, "响应成功~", promotionSpaceList);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", null);
            return result;
        }
    }

    /**
     * 添加&修改广告位
     * @param promotionSpace
     * @return
     */
    @PostMapping("/saveOrUpdatePromotionSpace")
    public ResponseResult saveOrUpdatePromotionSpace(@RequestBody PromotionSpace promotionSpace){
        ResponseResult result = null;
        try{
           if (null != promotionSpace.getId() && 0 < promotionSpace.getId()){
               promotionSpaceService.updatePromotionSpace(promotionSpace);
               result = new ResponseResult(true, 200, "修改成功~", null);
           } else {
               promotionSpaceService.savePromotionSpace(promotionSpace);
               result = new ResponseResult(true, 200, "保存成功~", null);
           }
           return result;
       } catch (Exception e){
           e.printStackTrace();
           result = new ResponseResult(true, 500, "发生异常~", null);
           return result;
       }
    }

    @GetMapping("/findPromotionSpaceById")
    public ResponseResult findPromotionSpaceById(@RequestParam Integer id){
        ResponseResult result = null;
        PromotionSpace promotionSpace = promotionSpaceService.findPromotionSpaceById(id);
        if (null != promotionSpace ){
            result = new ResponseResult(true, 200, "响应成功~", promotionSpace);
            return result;
        } else {
            result = new ResponseResult(false, 500, "查询为空~", null);
            return result;
        }
    }
}
